package com.lin.service.user;

import com.lin.dao.BaseDao;
import com.lin.dao.user.UserDao;
import com.lin.dao.user.UserDaoImpl;
import com.lin.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    public User getUserInfo(String userCode,String userPassword){
        Connection connect = null;
        User user = null;
        try {
            connect = BaseDao.getConnect();
            user = userDao.getUserInfo(connect, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connect,null,null);
        }
        if (null != user){
            if (!user.getUserPassword().equals(userPassword)){
                user = null;
            }
        }
        return user;
    }

    public boolean modifyPassword(Integer id, String userPassword) {
        Connection connection = null;
        boolean flag = false;
        connection = BaseDao.getConnect();
        try {
            flag = userDao.modifyPassword(connection, id, userPassword);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }

        return flag;
    }

    public List<User> getUserList(String userName, int userRole,int pageIndex,int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
          connection = BaseDao.getConnect();
          userList = userDao.getUserList(connection, userName, userRole, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }

    public Integer getUserCount(String userName, int userRole) {

        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnect();
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }
}
